package cn.zhutmj_bigmarket.domain.strategy.model.entity;

import cn.zhutmj_bigmarket.types.common.Constants;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @description： 策略规则实体
 * @author: zhutmj
 * @createTime: 2025/7/511:35
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class StrategyRuleEntity {

    /**  策略ID   **/
    private Long strategyId;
    /**   奖品ID【规则类型为策略，则不需要奖品ID】 **/
    private Integer awardId;
    /**  规则类型  **/
    private Integer ruleType;
    /**  抽象规则类型：【rule_random - 随机值计算、rule_lock - 抽奖几次后解锁、rule_luck_award - 幸运奖(兜底奖品)】    **/
    private String ruleModel;
    /**  抽象规则比值    **/
    private String ruleValue;
    /**  规则描述    **/
    private String ruleDesc;

    /**
     * 获取权重表
     * 数据案例；4000:102,103,104,105 5000:102,103,104,105,106,107 6000:102,103,104,105,106,107,108,109
     */
    public Map<String, List<Integer>> getRuleWeightValues(){
        if (!"rule_weight".equals(ruleModel)) return null;
        // 现在就是计算一个权重规则对应填充的奖品
        String[] ruleValus = ruleValue.split(Constants.SPACE);
        Map<String, List<Integer>> ruleWeightValues = new HashMap<String, List<Integer>>();
        for(String ruleVal : ruleValus){
            // 检查输入是否为空
            if(null == ruleVal || ruleVal.isEmpty()){
                return ruleWeightValues;
            }
            // 分割字符串以获取键和值
            String[] parts = ruleVal.split(Constants.COLON);
            if(parts.length != 2){
                throw new IllegalArgumentException("rule_weight rule_rule invalid input format" + ruleVal);
            }
            // 现在开始解析值
            String key = parts[0];
            String value = parts[1];
            List<Integer> weightValues = Arrays.stream(value.split(Constants.SPLIT))
                    .map(Integer::valueOf)
                    .collect(Collectors.toList());
            ruleWeightValues.put(key, weightValues);
        }
        return ruleWeightValues;
    }


}
